﻿Imports PhDir.Repository
Imports PhDirForm.Util
Imports PhDir.Model
Imports System.ComponentModel

Public Class TestingControl

    Private Const DataGridViewEmpty = 1

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or
                     ControlStyles.UserPaint Or
                     ControlStyles.AllPaintingInWmPaint, True)

        Me.DepartmentRepository = PhDir.Main.Instance.DepartmentRepository
        Me.UserRepository = PhDir.Main.Instance.UserRepository
        Me.PhoneRepository = PhDir.Main.Instance.PhoneRepository

        Me.SearchByComboBox.SelectedIndex = 0
    End Sub

    Private Property DepartmentRepository As DepartmentRepository
    Private Property UserRepository As UserRepository
    Private Property PhoneRepository As PhoneRepository
    Private Property Departments As List(Of Department)
    Private Property Phones As List(Of Phone)
    Public Property MainForm As PhDirForm.Form

    Private Sub Search_VisibleChanged(sender As Object, e As EventArgs) Handles MyBase.VisibleChanged
        Me.Phones = Me.PhoneRepository.SelectAll()
        Me.PopulatePhoneTable(Phones)
        Me.SearchTextBox.Text = String.Empty
        Me.TimeResultLabel.Text = String.Empty
    End Sub

    Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
        Dim timingResult As TimeSpan
        Dim searchMethod As PhoneRepository.SearchMethod

        Select Case Me.SearchByComboBox.SelectedItem
            Case "Plain phone number"
                searchMethod = New PhoneRepository.SearchMethod(AddressOf PhoneRepository.SearchPlain)
            Case "Encrypted phone number"
                searchMethod = New PhoneRepository.SearchMethod(AddressOf PhoneRepository.SearchEncrypted)
        End Select
        Me.Phones = Me.PhoneRepository.TimingSearch(Me.SearchTextBox.Text,
                                                            searchMethod,
                                                            timingResult)
        Me.PopulatePhoneTable(Phones)
        Me.TimeResultLabel.Text = String.Format("Time Execution = {0} seconds", timingResult.TotalSeconds)
    End Sub

    Private Sub PopulatePhoneTable(phones As List(Of Phone))
        Me.PhoneTable.Rows.Clear()

        For Each phone In phones
            Dim lastRow = Me.PhoneTable.Rows.Count
            Dim userId = phone.User.UserId
            Dim userName = phone.User.UserName
            Dim departmentId = phone.User.Department.DepartmentId
            Dim department = phone.User.Department.DepartmentName
            Dim phoneNumber = phone.PhoneNumber
            Me.PhoneTable.Rows.Add(userId, userName, departmentId,
                                   department, phoneNumber)
        Next
    End Sub
End Class
